package cms.com.yh.servlet;

import cms.common.Result;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能:
 * Author: yuhui
 * Date: 2021/4/8 17:33
 */
public class Servlet_CheckCode extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        createCheckCode(req,resp);
    }

    /**
    * 功能描述: 生成验证码
    * @Author: yuhui  2021/4/8 18:02
    */
    private void createCheckCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //createLineCaptcha(宽度,高度,字符个数,干扰元素个数)
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 6);
        //验证码图片中字符串 验证码字符串
        String code = lineCaptcha.getCode();
        req.getSession().setAttribute("code",code);
        Result re = new Result(code);
        resp.getWriter().print(JSONObject.toJSONString(re));

    }
}
